package com.wangzhen.codebase.reentrantlock;

import com.sun.jmx.remote.internal.ArrayQueue;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class BlockedQueue<T>{
    final Lock lock = new ReentrantLock();
    // 向队列里面插入数据的时候队列已经满了？
    final Condition full = lock.newCondition();

    final Condition empty = lock.newCondition();

    private Object[] objects = new Object[10];

    // 入队操作
    public void enq(T t) {
        try {
            lock.lock();
            // 如果队列满了，就进行等待
            while (objects.length == 10) {
                empty.await();
            }
            full.signalAll();
        } catch (Exception e) {

        } finally {
            lock.unlock();
        }
    }

    // 出队操作
    public void deq() {
        try {
            lock.lock();
            while (objects.length == 0) {
                full.await();
            }
            empty.signalAll();
        } catch (Exception e) {

        }
    }

}
